﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;
using NetJsWire.Lite.Converters;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Data
{
    [JsonObject( MemberSerialization = MemberSerialization.OptIn )]
    public class DirectStore : Store
    {
        private Api _api = new Api();
        public DirectStore() : this( null ) { }

        ///<summary>
        ///  Initializes a new instance of Store and adds it to the Container.
        ///</summary>
        ///<param name = "container"></param>
        public DirectStore( IContainer container )
            : base( container )
        {
            Xtype = "directstore";
            IdProperty = "id";
            SuccessProperty = "success";
            TotalProperty = "total";
            ParamsAsHash = true;
        }

        [JsonProperty( "api" )]
        [TypeConverter( typeof( ExpandableObjectConverter ) )]
        public Api Api
        {
            get { return _api; }
            set { _api = value; }
        }


        [JsonProperty( "directFn", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [DefaultValue( ( string ) null )]
        [Description( "Function to call when executing a request. directFn is a simple alternative to defining the api configuration-parameter for Store's which will not implement a full CRUD api." )]
        public string DirectFn { get; set; }

        [JsonProperty( "paramOrder", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( ( string ) null )]
        [Description( "A list of params to be executed server side. Specify the params in the order in which they must be executed on the server-side as either (1) an Array of String values, or (2) a String of params delimited by either whitespace, comma, or pipe." )]
        public string ParamOrder { get; set; }

        [JsonProperty( "paramsAsHash", DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( true )]
        [Description( "Send parameters as a collection of named arguments (defaults to true). Providing a paramOrder nullifies this configuration." )]
        public bool ParamsAsHash { get; set; }

        [JsonProperty( "idProperty", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( "id" )]
        [Description( "Name of the property within a row object that contains a record identifier value. Defaults to id" )]
        public string IdProperty { get; set; }

        [Browsable( false )]
        public override DataProxy Proxy
        {
            get
            {
                return base.Proxy;
            }
            set
            {
                base.Proxy = value;
            }
        }

        [Browsable( false )]
        public override DataReader Reader
        {
            get
            {
                return base.Reader;
            }
            set
            {
                base.Reader = value;
            }
        }

        [JsonProperty( "root", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( ( string ) null )]
        [Description( "Required. The name of the property which contains the Array of row objects. Defaults to undefined. An exception will be thrown if the root property is undefined. The data packet value for this property should be an empty array to clear the data or show no data." )]
        public string Root { get; set; }

        [JsonProperty( "successProperty", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( "success" )]
        [Description( "Name of the property from which to retrieve the success attribute. Defaults to success." )]
        public string SuccessProperty { get; set; }

        [JsonProperty( "totalProperty", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( "total" )]
        [Description( "Name of the property from which to retrieve the total number of records in the dataset. This is only needed if the whole dataset is not passed in one go, but is being paged from the remote server. Defaults to total." )]
        public string TotalProperty { get; set; }

        [JsonProperty( "xtype", NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Include )]
        [DefaultValue( "directstore" )]
        [TypeConverter( typeof( XtypeTypeConverter ) )]
        public override string Xtype
        {
            get
            {
                return base.Xtype;
            }
            set
            {
                base.Xtype = value;
            }
        }

        public bool ShouldSerializeApi()
        {
            return !string.IsNullOrWhiteSpace( Api.Create ) || !string.IsNullOrWhiteSpace( Api.Destroy ) || !string.IsNullOrWhiteSpace( Api.Read ) || !string.IsNullOrWhiteSpace( Api.Update );
        }

        public void ResetApi()
        {
            Api = new Api();
        }
    }
}
